#include "hubwidget.h"
#include "ui_hubwidget.h"

#include <QDesktopServices>
#include <QDir>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QPainter>
#include <QMessageBox>
#include <QMimeData>

#include "settinghandler.h"
#include "widget.h"

static HubWidget* hub_widget = nullptr;
HubWidget* HubWidget::get_instance()
{
    if (nullptr == hub_widget)
        hub_widget = new HubWidget(nullptr);
    return hub_widget;
}

HubWidget::HubWidget(QWidget *main_widget)
    :QWidget(nullptr), ui(new Ui::HubWidget), main_widget_(main_widget)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_TranslucentBackground);  // 设置窗口背景透明
    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
    resize(SettingHandler::get_instance()->get_border_size() + 10 * SettingHandler::get_instance()->get_dx(), SettingHandler::get_instance()->get_border_size() + 5 * SettingHandler::get_instance()->get_dx() + 10);  // 加的10是给底部箭头留空间
    // 接收鼠标拖动事件
    setAcceptDrops(true);

    hub_lnk_handler = new HubLNKHandler(Widget::get_program_dir() + "/hub_links/", Widget::get_program_dir() + "/hub_init.xq", this);
}

void HubWidget::refresh_all()
{
    auto lnk_list = hub_lnk_handler->get_list();
    for (int i = 0; i < lnk_list.size(); ++i)
    {
        lnk_list.at(i)->move(SettingHandler::get_instance()->get_border_size() + (i % 10) * SettingHandler::get_instance()->get_dx(), SettingHandler::get_instance()->get_border_size() + (i / 10) * SettingHandler::get_instance()->get_dx());
        lnk_list.at(i)->show();
    }
}

// 刷新界面并显示
void HubWidget::update_and_show(int center_x)
{
    pointer_x = center_x;
    refresh_all();
    show();
}

void HubWidget::open_file(const QString& absolute_path)
{
    bool ret = QDesktopServices::openUrl(QUrl::fromLocalFile(absolute_path));
    if (!ret)
        QMessageBox::critical(nullptr, "打开错误", "我们遇到了一些意想不到的错误，\n打开 " + absolute_path + " 失败");
}

void HubWidget::dragEnterEvent(QDragEnterEvent *event)
{
    event->acceptProposedAction();    // 接收拖拽信号
}

// 文件路径可能要改
void HubWidget::dropEvent(QDropEvent *event)
{
    auto index = ((int) event->position().y() - SettingHandler::get_instance()->get_border_size() / 2) / SettingHandler::get_instance()->get_dx() * 10
            + ((int) event->position().x() + SettingHandler::get_instance()->get_lnk_size() / 2) / SettingHandler::get_instance()->get_dx();

    qDebug() << "拖进来了";

    if (index > hub_lnk_handler->get_size())
        index = hub_lnk_handler->get_size();

    for (const auto &url : event->mimeData()->urls())
    {
        QString absolute_path =  url.toLocalFile();
        hub_lnk_handler->add_lnk(index, absolute_path);
    }

    refresh_all();
}

void HubWidget::mousePressEvent(QMouseEvent *)
{
    this_is_clicked = true;
}

void HubWidget::mouseReleaseEvent(QMouseEvent *)
{
    if (this_is_clicked)
        emit clicked_space();
}

void HubWidget::mouseMoveEvent(QMouseEvent *)
{
    this_is_clicked = false;
}

void HubWidget::paintEvent(QPaintEvent *)
{
    /*画登陆界面的圆角矩形背景*/
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    QPen pen(SettingHandler::get_instance()->get_background_color());
    pen.setWidth(0);
    painter.setPen(pen);
    QBrush brush(SettingHandler::get_instance()->get_background_color());
    painter.setBrush(brush);

    QRectF rect(0, 0, width(), height() - 10);
    painter.drawRoundedRect(rect, 15, 15);

    // 画窗口下方的三角形
    const QPointF points[3] =
    {
        QPointF(pointer_x - 10, height() - 10),
        QPointF(pointer_x + 10, height() - 10),
        QPointF(pointer_x, height())
    };

    painter.drawConvexPolygon(points, 3);
}

HubWidget::~HubWidget()
{
    delete ui;
}
